package com.sugon.controller.spare;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.SpareDevEntity;
import com.sugon.entity.SparePartEntity;
import com.sugon.service.SpareDevService;
import com.sugon.service.SparePartService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 备件设备关联Controller
 *
 * @author sugon
 * @date 2019-07-17 15:19:36
 */
@Controller
@RequestMapping("sparedev")
public class SpareDevController {

    @Autowired
    private SpareDevService spareDevService;
    @Autowired
    private SparePartService sparePartService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sparedev:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<SpareDevEntity> spareDevList = spareDevService.queryList(query);
        int total = spareDevService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(spareDevList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sparedev:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        SpareDevEntity spareDev = spareDevService.queryObject(id);

        return R.ok().put("spareDev", spareDev);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sparedev:save")
    @ResponseBody
    public R save(@RequestBody SpareDevEntity spareDev) {
        // 校验备件本身是否存在
        String spareId = spareDev.getSpareId();
        SparePartEntity sparePart = sparePartService.queryObject(spareId);
        if (sparePart == null) {
            return R.error("备件信息不存在，请确认！");
        }
        // 校验替换件信息是否存在
        String spareRepId = spareDev.getSpareRepId();
        SparePartEntity repSparePart = sparePartService.queryObject(spareRepId);
        if (repSparePart == null) {
            return R.error("替换件信息不存在，请确认！");
        }
        // 不可以自己添加自己作为替换件
        if (spareId.equals(spareRepId)) {
            return R.error("禁止添加备件自身作为替换件！");
        }
        // 是否已添加过
        boolean isAdded = spareDevService.isAdded(spareId, spareRepId);
        if (isAdded) {
            return R.error("该替换件已添加过，无需重复添加！");
        }
        return spareDevService.save(spareDev) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sparedev:update")
    @ResponseBody
    public R update(@RequestBody SpareDevEntity spareDev) {
        spareDevService.update(spareDev);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sparedev:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        spareDevService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SpareDevEntity> list = spareDevService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 审核
     */
    @RequestMapping("/review")
    @RequiresPermissions("sparedev:review")
    @ResponseBody
    public R review(@RequestParam("id") String id, @RequestParam("checkFlag") String checkFlag) {
        spareDevService.review(id, checkFlag);

        return R.ok();
    }
}
